/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Return.java
 *
 * Created on Sep 16, 2010, 3:10:32 PM
 */

package COMIC_BOOK_STORE;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author izzah
 */
public class BOOK_RETURN extends javax.swing.JFrame {

    Connection con;
    Statement stat;
     ResultSet rset;
     ResultSet rset2;
     ResultSet rs2;
   

    /** Creates new form Return */
    public BOOK_RETURN() throws SQLException {
         super("RETURN BOOK");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        memberDetailsLabel = new javax.swing.JLabel();
        bookDetailsLabel = new javax.swing.JLabel();
        memberCodeLabel = new javax.swing.JLabel();
        bookCodeLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        memberCodeTextField = new javax.swing.JTextField();
        bookCodeTextField = new javax.swing.JTextField();
        memberCodeLabel2 = new javax.swing.JLabel();
        firstNameLabel = new javax.swing.JLabel();
        lastNameLabel = new javax.swing.JLabel();
        icNumberLabel = new javax.swing.JLabel();
        creditBalanceLabel = new javax.swing.JLabel();
        contactNumberLabel = new javax.swing.JLabel();
        displayMemberCode = new javax.swing.JLabel();
        displayFirstName = new javax.swing.JLabel();
        displayLastName = new javax.swing.JLabel();
        displayIcNumber = new javax.swing.JLabel();
        displayContactNumber = new javax.swing.JLabel();
        displayCreditBalance = new javax.swing.JLabel();
        bookCodeLabel2 = new javax.swing.JLabel();
        totalChargeLabel = new javax.swing.JLabel();
        displayBookCode = new javax.swing.JLabel();
        displayTotalCharge = new javax.swing.JLabel();
        rentInDateLabel = new javax.swing.JLabel();
        enterButton = new javax.swing.JButton();
        OKbutton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        rentIndateTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        memberDetailsLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        memberDetailsLabel.setText("Member Details : ");
        memberDetailsLabel.setBounds(50, 140, 140, 20);
        jLayeredPane1.add(memberDetailsLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookDetailsLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        bookDetailsLabel.setText("Book Details : ");
        bookDetailsLabel.setBounds(450, 140, 140, 30);
        jLayeredPane1.add(bookDetailsLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        memberCodeLabel.setText("Member Code");
        memberCodeLabel.setBounds(60, 30, 120, 30);
        jLayeredPane1.add(memberCodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookCodeLabel.setText("Book Code");
        bookCodeLabel.setBounds(60, 70, 100, 30);
        jLayeredPane1.add(bookCodeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(0, 120, 900, -1);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        memberCodeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                memberCodeTextFieldActionPerformed(evt);
            }
        });
        memberCodeTextField.setBounds(250, 30, 180, 30);
        jLayeredPane1.add(memberCodeTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);
        bookCodeTextField.setBounds(250, 70, 180, 30);
        jLayeredPane1.add(bookCodeTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        memberCodeLabel2.setText("Member Code");
        memberCodeLabel2.setBounds(50, 190, 110, 30);
        jLayeredPane1.add(memberCodeLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        firstNameLabel.setText("First Name");
        firstNameLabel.setBounds(50, 230, 100, 30);
        jLayeredPane1.add(firstNameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lastNameLabel.setText("Last Name");
        lastNameLabel.setBounds(50, 270, 90, 30);
        jLayeredPane1.add(lastNameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        icNumberLabel.setText("IC Number");
        icNumberLabel.setBounds(50, 310, 90, 30);
        jLayeredPane1.add(icNumberLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        creditBalanceLabel.setText("Credit Balance");
        creditBalanceLabel.setBounds(50, 390, 120, 30);
        jLayeredPane1.add(creditBalanceLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        contactNumberLabel.setText("Contact Number");
        contactNumberLabel.setBounds(50, 350, 120, 30);
        jLayeredPane1.add(contactNumberLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayMemberCode.setText("x");
        displayMemberCode.setBounds(200, 180, 130, 30);
        jLayeredPane1.add(displayMemberCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayFirstName.setText("x");
        displayFirstName.setBounds(200, 220, 150, 30);
        jLayeredPane1.add(displayFirstName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayLastName.setText("x");
        displayLastName.setBounds(200, 260, 150, 30);
        jLayeredPane1.add(displayLastName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayIcNumber.setText("x");
        displayIcNumber.setBounds(200, 300, 150, 30);
        jLayeredPane1.add(displayIcNumber, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayContactNumber.setText("x");
        displayContactNumber.setBounds(200, 340, 150, 30);
        jLayeredPane1.add(displayContactNumber, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayCreditBalance.setText("x");
        displayCreditBalance.setBounds(200, 380, 140, 30);
        jLayeredPane1.add(displayCreditBalance, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookCodeLabel2.setText("Book Code");
        bookCodeLabel2.setBounds(450, 190, 100, 30);
        jLayeredPane1.add(bookCodeLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        totalChargeLabel.setText("Total Charge");
        totalChargeLabel.setBounds(450, 230, 120, 30);
        jLayeredPane1.add(totalChargeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayBookCode.setText("x");
        displayBookCode.setBounds(590, 190, 140, 30);
        jLayeredPane1.add(displayBookCode, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayTotalCharge.setText("x");
        displayTotalCharge.setBounds(590, 230, 130, 30);
        jLayeredPane1.add(displayTotalCharge, javax.swing.JLayeredPane.DEFAULT_LAYER);

        rentInDateLabel.setText("Rent In Date");
        rentInDateLabel.setBounds(450, 280, 110, 30);
        jLayeredPane1.add(rentInDateLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        enterButton.setText("FIND");
        enterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enterButtonActionPerformed(evt);
            }
        });
        enterButton.setBounds(580, 40, 110, 40);
        jLayeredPane1.add(enterButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        OKbutton.setText("RETURN");
        OKbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKbuttonActionPerformed(evt);
            }
        });
        OKbutton.setBounds(590, 430, 110, 40);
        jLayeredPane1.add(OKbutton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cancelButton.setText("CANCEL");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        cancelButton.setBounds(720, 430, 110, 40);
        jLayeredPane1.add(cancelButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel1.setBounds(24, 170, 340, 260);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        rentIndateTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rentIndateTextFieldActionPerformed(evt);
            }
        });
        rentIndateTextField.setBounds(590, 280, 180, 30);
        jLayeredPane1.add(rentIndateTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel2.setBounds(410, 170, 420, 240);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/Background5 copy.jpg"))); // NOI18N
        jLabel3.setText("jLabel3");
        jLabel3.setBounds(-40, 0, 940, 500);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void rentIndateTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
}                                                   

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        new MAIN_MENU().setVisible(true);
        this.setVisible(false);
}                                            

    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            

        String choice1 = memberCodeTextField.getText();
        String choice2 = bookCodeTextField.getText();

        double  chargeextra;
        String  Credit = "";
        float   credit = 0;
        String  bcharge = null;
        String  returnDate = "";
        Double total = 0.0;

        String query1 = "SELECT * FROM TRANSACTION WHERE BOOK_CODE ='"+choice2+"'";
        String query2 = "SELECT * FROM MEMBER WHERE MEMBER_CODE='"+choice1+"'";
        String query3 = "select * from book where book_code = '" + choice2 + "'";
        String query4 = "UPDATE TRANSACTION SET TOTAL_CHARGE   = '"+total+"'";

        if (choice1.equals("") && choice2.equals("")) {
            JOptionPane.showMessageDialog(null,"Please Fill first!" );
        }

        else
        {
            try
            {
                stat = con.createStatement();
                rset = stat.executeQuery(query1);

                System.out.println("query 1 - " + query1);

                while(rset.next()) {

                    displayBookCode.setText(rset.getString("BOOK_CODE"));
                    rentIndateTextField.setText(rset.getString("RETURN_DATE"));
                    returnDate = (String)rset.getString("RETURN_DATE").trim();

                }

            }//end try

            catch(SQLException ex)

            {
                Logger.getLogger(BOOK_RETURN.class.getName()).log(Level.SEVERE, null, ex);
            }

             try
            {
                Statement stat1;
                ResultSet rset1;

                stat1 = con.createStatement();
                rset1 = stat1.executeQuery(query2);

                System.out.println("query 2 - " + query2);

                while(rset1.next()) {
                    displayMemberCode.setText(rset1.getString("MEMBER_CODE"));
                    displayFirstName.setText(rset1.getString("MEMBER_FNAME"));
                    displayLastName.setText(rset1.getString("MEMBER_LNAME"));
                    displayIcNumber.setText(rset1.getString("MEMBER_ICNO"));
                    displayContactNumber.setText(rset1.getString("MEMBER_CONTACTNO"));
                    displayCreditBalance.setText(rset1.getString("MEMBER_CREDIT"));
                    Credit = (rset1.getString(14)).trim();

                    System.out.println("check point 1");
                }
            }//end try

            catch(SQLException ex)

            {
                Logger.getLogger(BOOK_RETURN.class.getName()).log(Level.SEVERE, null, ex);
            }

            try
            {
                Statement stat1;
                ResultSet rset1;

                stat1 = con.createStatement();
                rset1 = stat1.executeQuery(query3);

                System.out.println("query 3 - " + query3);

                while(rset1.next()) {
                    bcharge = rset1.getString("BOOK_COST");
                    System.out.println("check point 2");
                }

            }

            catch(SQLException ex)

            {
                Logger.getLogger(BOOK_RETURN.class.getName()).log(Level.SEVERE, null, ex);
            }

            String day = returnDate.substring(0,2);
                    String mon = returnDate.substring(3,6);
                    String year = returnDate.substring(7,11);


                    if (mon.equals("Jan"))
                        mon = "01";
                    if (mon.equals("Feb"))
                        mon = "02";
                    if (mon.equals("Mar"))
                        mon = "03";
                    if (mon.equals("Apr"))
                        mon = "04";
                    if (mon.equals("May"))
                        mon = "05";
                    if (mon.equals("Jun"))
                        mon = "06";
                    if (mon.equals("Jul"))
                        mon = "07";
                    if (mon.equals("Aug"))
                        mon = "08";
                    if (mon.equals("Sep"))
                        mon = "09";
                    if (mon.equals("Oct"))
                        mon = "10";
                    if (mon.equals("Nov"))
                        mon = "11";
                    if (mon.equals("Dec"))
                        mon = "12";

                    int Day = Integer.parseInt(day);
                    int Mon = Integer.parseInt(mon);
                    int Year = Integer.parseInt(year);

                    System.out.println("Day = " + Day + " Mon = " + Mon + " Year = " + Year);

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                    String today = sdf.format(new Date(System.currentTimeMillis()));

                    day = today.substring(8,10);
                    mon = today.substring(5,7);
                    year = today.substring(0,4);

                    int Day2 = Integer.parseInt(day);
                    int Mon2 = Integer.parseInt(mon);
                    int Year2 = Integer.parseInt(year);

                    System.out.println("Day2 = " + Day2 + " Mon2 = " + Mon2 + " Year2 = " + Year2);

                    int daydiff = 0;
                    int mondiff = 0;
                    int yeardiff = 0;
                    int totaldiff = 0;

                    if (Day2 >= Day)
                    {
                        daydiff = Day2 - Day;
                    }

                    else
                    {
                        daydiff = (30 - Day) + Day2;
                        mondiff += mondiff;
                    }

                    if (Mon2 >= Mon)
                    {
                        mondiff = Mon2 - Mon;
                    }

                    else
                    {
                        mondiff = 12 - Mon + Mon2;
                    }

                    yeardiff = Year2 - Year;

                    totaldiff = yeardiff*365 + mondiff*30 + daydiff;

                    chargeextra  = totaldiff * 0.50;

                    credit = Float.valueOf(Credit).floatValue();

                    credit  -= chargeextra;

                    Double temp = Double.parseDouble(bcharge);

                    total = chargeextra + temp;

                    try
                    {
                        stat = con.createStatement();
                        stat.executeUpdate(query4);
                        stat.close();
                    }

                    catch(SQLException ex)
                    {
                        Logger.getLogger(BOOK_RETURN.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    displayTotalCharge.setText(""+total);

        }//end else
}                                           

    private void memberCodeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
}                                                   

    private void OKbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                         

          String query = null;

        String choice1 = memberCodeTextField.getText();
        String choice2 = bookCodeTextField.getText();

        if(choice1.equals("")|| choice2.equals(""))
        {
            JOptionPane.showMessageDialog(null, "Nothing to return!!");
        }
        else
        {
            query = "SELECT * FROM MEMBER,TRANSACTION WHERE MEMBER.MEMBER_CODE='"+choice1+"' AND TRANSACTION.BOOK_CODE ='"+choice2+"'";

            String message = "Are you sure to return?";
            String title = "RETURN BOOK";

            int reply = JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION);

             if(reply == JOptionPane.YES_OPTION) {
                try {
                    stat = con.createStatement();
                     stat.executeUpdate(query);

                      JOptionPane.showMessageDialog(null, "Return Successful");


                } catch (SQLException ex) {
                    Logger.getLogger(BOOK_RETURN.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(null, "Return Failed!");
                }
             }
        }
        // TODO add your handling code here:
    }                                        



     private void connect() throws SQLException
    {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        System.out.println("Driver Loaded");
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");
        System.out.println("Database Connected");

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new BOOK_RETURN().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(BOOK_RETURN.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton OKbutton;
    private javax.swing.JLabel bookCodeLabel;
    private javax.swing.JLabel bookCodeLabel2;
    private javax.swing.JTextField bookCodeTextField;
    private javax.swing.JLabel bookDetailsLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel contactNumberLabel;
    private javax.swing.JLabel creditBalanceLabel;
    private javax.swing.JLabel displayBookCode;
    private javax.swing.JLabel displayContactNumber;
    private javax.swing.JLabel displayCreditBalance;
    private javax.swing.JLabel displayFirstName;
    private javax.swing.JLabel displayIcNumber;
    private javax.swing.JLabel displayLastName;
    private javax.swing.JLabel displayMemberCode;
    private javax.swing.JLabel displayTotalCharge;
    private javax.swing.JButton enterButton;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JLabel icNumberLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JLabel memberCodeLabel;
    private javax.swing.JLabel memberCodeLabel2;
    private javax.swing.JTextField memberCodeTextField;
    private javax.swing.JLabel memberDetailsLabel;
    private javax.swing.JLabel rentInDateLabel;
    private javax.swing.JTextField rentIndateTextField;
    private javax.swing.JLabel totalChargeLabel;
    // End of variables declaration                   

}
